﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ChannelDownloadingProject;

using YoutubeInformationCRUD;

namespace ChannelDownloadingProject
{
	public class Program
	{
		private static void Main(string[] args)
		{
			try {
				Configuration.Instance.Init();
				YouTubeVideoHelper.addToStartup();
			} catch (Exception ex) {
				Console.WriteLine(ex.ToString());
				YouTubeVideoHelper.SendMail(ex.ToString());
				throw ex;
			}
			
			while(true)
			{
				try {
					// get all channel from database
					EntityBase.ConnectionString = "Data Source='"+Configuration.Instance.DbPath+"'";
					Console.WriteLine(EntityBase.ConnectionString);
					YoutubeChannelRepository channelRepository = new YoutubeChannelRepository();
					YoutubeVideoRepository videoRepository = new YoutubeVideoRepository();
					var allChannel = channelRepository.SelectByIsActive(true);
					foreach (var channel in allChannel) {
						Console.WriteLine("checking channel " + channel.Description + " Mode: " + channel.Mode + " : " + channel.URL);
						//step by step
						//step 1 : get all video on channel that did not download yet
						var allVideoOnChannel = YouTubeVideoHelper.GetVideos(channel.ChannelId);
						if(allVideoOnChannel!=null && allVideoOnChannel.Count > 0){
							//save to db
							YouTubeVideoHelper.InsertIfNotExist(allVideoOnChannel,videoRepository);
							allVideoOnChannel = videoRepository.SelectByDownloaded(false);
							if(allVideoOnChannel==null || allVideoOnChannel.Count <= 0) continue;
							foreach (var video in allVideoOnChannel) {
								if(channel.Mode == Configuration.CHANNEL_MODE_VIDEO_ONLY){
									Console.WriteLine("starting download video of " + video.Title);
									try {
										YouTubeVideoHelper.DownloadVideo(video,videoRepository);
									} catch (Exception ex) {
										Console.WriteLine(ex.ToString());
										YouTubeVideoHelper.SendMail(ex.ToString());
									}
									
								}else if(channel.Mode == Configuration.CHANNEL_MODE_AUDIO_ONLY){
									Console.WriteLine("starting download audio of " + video.Title);
									try {
										YouTubeVideoHelper.DownloadAudio(video,videoRepository);
									} catch (Exception ex) {
										Console.WriteLine(ex.ToString());
										YouTubeVideoHelper.SendMail(ex.ToString());
									}
								}else if(channel.Mode == Configuration.CHANNEL_MODE_BOTH){
									Console.WriteLine("starting download audio and video of " + video.Title);
									try {
										YouTubeVideoHelper.DownloadBothAudioAndVideo(video,videoRepository);
									} catch (Exception ex) {
										Console.WriteLine(ex.ToString());
										YouTubeVideoHelper.SendMail(ex.ToString());
									}
								}else{
									//custom mode
								}
							}
						}
					}
					//then sleep  then continue
					Console.WriteLine("Sleep a little bit !");
					Thread.Sleep(Configuration.Instance.SleepTime * 1000);
				} catch (Exception ex) {
					Console.WriteLine(ex.ToString());
					YouTubeVideoHelper.SendMail(ex.ToString());
				}
			}
		}
		
		
	}
}